self.onmessage = (e: { data: { chunk: any; type: any } }) => {
  const { chunk } = e.data
  let type = 1
  const param = {
    sessionId: "",
    content: "",
    reasoning_content: "",
    total_tokens: 0,
    thinking_time: 0
  }
  const titleData = {
    id:0,
    userId:0,
    title:"",
    updateTime:0
  }
  chunk.split('\n\n').forEach((block: any) => {
    if (block.startsWith('data:') && !block.includes('[DONE]')) {
      try {
        const data = JSON.parse(block.replace('data:', '').trim())
        // console.log('实时数据块:', data);
        if (data.sessionId) {
          param.sessionId = data.sessionId
        } else if (data.conversationContent) {
          param.content = data.conversationContent
        } else if (data.reasoningContent) {
          param.reasoning_content = data.reasoningContent
        } else if (data.tokens) {
          param.total_tokens = data.tokens
        } else if (data.thinkingTime) {
          param.thinking_time = data.thinking_time
        }else if (data.id&&data.userId&& data.title){
          titleData.id = data.id
          titleData.userId = data.userId
          titleData.title = data.title
          titleData.updateTime= data.updateTime
          type = 2
        }
        // 拼接完整响应（可选）
        // fullResponse += data.choices[0].delta?.content || '';
      } catch (e) {
        console.warn('解析失败:', block);
      }
    } else if (block.includes('[DONE]')) {
      self.postMessage({ type: 'DONE', content: param })
    }
  });
  // console.log(param)
  if (type==2){
  self.postMessage({ type: 'sessionTitleData', content: titleData })
  }else {
  self.postMessage({ type: 'sessionData', content: param })
  }

}

// const msg = chunk.replace("data: ", '')
// console.log(msg)
// const data = JSON.parse(msg)
// if (data.sessionId) {
//   self.postMessage({ type: 'sessionData', content: data.sessionId })
// } else if (data.content) {
//   self.postMessage({ type: 'content', content: data.content })
// } else if (data.reasoning_content) {
//   self.postMessage({ type: 'think', content: data.reasoning_content })
// }
// 在这里实现复杂的解析逻辑
// if (type === 'markdown') {
//   //   const parsed = marked.parse(chunk)
//   self.postMessage({ type: 'markdown', content: chunk })
// } else if (type === 'code') {
//   //   const highlighted = hljs.highlightAuto(chunk).value
//   self.postMessage({ type: 'code', content: chunk })
// } else {


// }